package com.top.zk.pandachannel.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.top.zk.pandachannel.IMapBiz;
import com.top.zk.pandachannel.R;
import com.top.zk.pandachannel.adapter.EyeRecyclerviewAdapter;
import com.top.zk.pandachannel.bean.EyeBean;
import com.top.zk.pandachannel.bean.EyeBean2;
import com.top.zk.pandachannel.help.SpacesItemDecoration;
import com.top.zk.pandachannel.util.URLConstants;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class PandaEyeFragment extends Fragment {

    private Context mContext = null;
    private ImageView imageView_eye;
    private TextView textView_eye;
    private String imgURL;
    private String textURL;
    private RecyclerView recyclerView_eye;
    private EyeRecyclerviewAdapter mAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    //大图片加载
    public void initBigimg(){
        Retrofit retrofit = new Retrofit.Builder().baseUrl(URLConstants.BASEURL)
                .addConverterFactory(GsonConverterFactory.create()).build();
        IMapBiz iMapBiz = retrofit.create(IMapBiz.class);
        Call<EyeBean> call = iMapBiz.getEyeBeanjson();
        call.enqueue(new Callback<EyeBean>() {
            @Override
            public void onResponse(Call<EyeBean> call, Response<EyeBean> response) {
                if (response.isSuccess()) {
                    EyeBean body = response.body();

                    for (int i = 0; i < body.getData().getBigImg().size(); i++) {
                        imgURL = body.getData().getBigImg().get(i).getImage();
                        textURL = body.getData().getBigImg().get(i).getTitle();
                    }
                    Glide.with(mContext).load(imgURL).into(imageView_eye);
                    textView_eye.setText(textURL);
                }
            }

            @Override
            public void onFailure(Call<EyeBean> call, Throwable t) {
                t.printStackTrace();
            }
        });

    }

    //列表数据加载
    private void initData() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(URLConstants.BASEURL_EYELIST)
                .addConverterFactory(ScalarsConverterFactory.create()).build();
        IMapBiz iMapBiz = retrofit.create(IMapBiz.class);
        Call<String> call = iMapBiz.getEyeJsonString();

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccess()){
                    String body = response.body();
                    Gson gson = new Gson();
                    EyeBean2 eyeBean = gson.fromJson(body, EyeBean2.class);
                    List<EyeBean2.ListBean> list = eyeBean.getList();
                    //创建适配器
                    mAdapter = new EyeRecyclerviewAdapter(mContext,list);
                    //设置适配器
                    recyclerView_eye.setAdapter(mAdapter);
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_panda_eye, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        imageView_eye = (ImageView) view.findViewById(R.id.imageView_eye);
        textView_eye = (TextView) view.findViewById(R.id.textView_eye);

        recyclerView_eye = (RecyclerView) view.findViewById(R.id.recyclerView_eye);
        LinearLayoutManager layoutManager =
                new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
        recyclerView_eye.setLayoutManager(layoutManager);
        recyclerView_eye.addItemDecoration(new SpacesItemDecoration(2));
        initBigimg();

        initData();
    }
}
